package model;

import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Observable;
import main.WinkelApplication;

public class Basket extends Observable {

    private final Map<Product, Integer> products;

    public Basket() {
        super();
        products = new LinkedHashMap<Product, Integer>();
    }

    public void addProduct(Product product) {
        
        // check if product is allready added to the basket
        if (products.containsKey(product)) {
            if(products.get(product)+1 > product.getStock()){
                WinkelApplication.msgbox("U kunt geen extra producten toevoegen omdat er niet meer\nvan deze producten op voorraad zijn");
            } else {
                products.put(product, products.get(product) + 1);
            }
        } else {
            products.put(product, 1);
        }
        setChanged();
        notifyObservers();
    }
    
    public void deleteProduct(Product product){
        products.remove(product);
        setChanged();
        notifyObservers();
    }

    public void empty() {
        products.clear();
        setChanged();
        notifyObservers();
    }

    public List<Product> getProducts() {
        List<Product> list = new LinkedList<Product>(products.keySet());
        return Collections.unmodifiableList(list);
    }

    public int getProductAmount(Product product) {
        return products.get(product);
    }

    public int size() {
        return products.size();
    }

    public String getTotalCosts() {
        double total = 0.0;
        for (Entry<Product, Integer> entry : products.entrySet()) {
            total += entry.getKey().getPrice() * entry.getValue();
        }
        String formatted =  String.valueOf(total);
        if((formatted.length() - formatted.indexOf('.'))<3){
            return formatted + "0";
        } else {
            return formatted;
        }        
    }
}
